package com.visionblue.business.ship.page.outer

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.DefaultObserver
import com.visionblue.business.ship.model.bean.response.ApplicationDetail
import com.visionblue.business.ship.page.collection.CollectionActivity
import com.visionblue.business.ship.page.dialog.SelectionTransportAdapter
import com.visionblue.business.ship.page.dialog.SelectionTransportDialog
import com.visionblue.business.ship.page.dialog.TransferOutDialog
import com.visionblue.business.ship.util.toast
import kotlinx.android.synthetic.main.activity_collection.*
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 * @author: xlh
 * @date: 2020-06-08 7:57 AM
 * @desc:
 */
class OuterActivity : CollectionActivity() {

    private lateinit var outerViewModel: OuterViewModel

    private val mSlideDialog: TransferOutDialog by lazy {
        TransferOutDialog(this)
    }

    private val selectionTransportDialog: SelectionTransportDialog by lazy {
        SelectionTransportDialog(this)
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        tvInfoTitle.text = "转出对象"
        outerViewModel = getViewModel()
        btnSelectReceived.setText(R.string.select_outer_obj)
        btnSelectReceived.setOnClickListener {
            outerViewModel.getTransferOutTasks()
        }
    }

    override fun initData() {
        super.initData()

        outerViewModel.transferOuterViewState.observe(this, DefaultObserver(errorAction = {
            mSlideDialog.showError()
        }, loadingAction = {
            mSlideDialog.showLoading()
        }, action = {
            val data = it.data
            if (data != null) {
                onDetailsGot(
                    it.data.hasNext(),
                    it.data.hasPre(), data.content
                )
            }
        }))

        outerViewModel.operateTaskEvent.observe(this, DefaultObserver(finallyAction = {
            mDialog.dismiss()
        }, loadingAction = {
            mDialog.show()
        }, action = {
            toast { "撤销成功" }
            outerViewModel.getApplicationsAfterOperation()
        }))

        outerViewModel.transporters.observe(this, DefaultObserver(loadingAction = {
            selectionTransportDialog.showLoading()
        }, errorAction = {
            selectionTransportDialog.showError()
        }, action = {
            val content = it.data
            if (content != null && content.isNotEmpty()) {
                selectionTransportDialog.showContent(
                    it.data,
                    object : SelectionTransportAdapter.OnItemClickListener {
                        override fun onItemClick(shipNo: String?) {
                            outerViewModel.createTransportApplication(shipNo)
                        }
                    })
            } else {
                selectionTransportDialog.showEmpty()
            }
        }))

        outerViewModel.simpleEvent.observe(this, DefaultObserver(loadingAction = {
            mDialog.show()
        }, finallyAction = {
            mDialog.dismiss()
        }, action = {
            toast { "创建成功" }
        }))

    }

    override fun isOutput(): Boolean {
        return true
    }


    private fun onDetailsGot(hasNext: Boolean, hasPre: Boolean, content: List<ApplicationDetail>) {
        val listener = object :
            TransferOutDialog.OnViewsClickListener {

            override fun onRecallClick(id: String?) {
                outerViewModel.recallOut(id)
            }

            override fun onItemClick(detail: ApplicationDetail) {
                showSelectedView(detail)
            }

            override fun onPreClick() {
                outerViewModel.prePage()
            }

            override fun onNextClick() {
                outerViewModel.nextPage()
            }

            override fun onDismiss() {
                outerViewModel.resetPageNum()
            }

            override fun onStartClick() {
                outerViewModel.getTransporters()
            }
        }

        if (content.isNotEmpty()) {
            mSlideDialog.showContent(content[0], hasNext, hasPre, listener)
        } else {
            if (!hasPre && !hasNext) {
                mSlideDialog.showEmpty(listener)
            }
        }
    }

    private fun showSelectedView(detail: ApplicationDetail) {
        tvMainShipName.visibility = View.VISIBLE
        tvInfoTitle.visibility = View.VISIBLE
        btnSelectReceived.visibility = View.GONE

        tvMainShipName.text = detail.shipName ?: "-"
        tvMainShipName.tag = detail

        launch {
            mViewModel.updateCachedOrder(detail.id, detail.shipName)
                .join()
            mViewModel.cacheApplicationDetail(detail)
        }
    }

    companion object {
        fun start(context: Context) {
            Intent(context, OuterActivity::class.java)
                .run {
                    context.startActivity(this)
                }
        }
    }
}